package com.jz.beijianbao.legalperson.service.impl;

import com.jz.beijianbao.legalperson.bean.*;
import com.jz.beijianbao.legalperson.mapper.LegalPersonMapper;
import com.jz.beijianbao.legalperson.personEnum.PersonEnum;
import com.jz.beijianbao.legalperson.service.LegalPersonService;
import com.jz.util.AllRecordsVo;
import com.jz.util.ResultBuildVO;
import com.jz.util.ResultVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class LegalPersonServiceImpl implements LegalPersonService {

    @Autowired
    private LegalPersonMapper legalPersonMapper;

    @Override
    public ResultVO queryXiaLa() {
        List<User> user = legalPersonMapper.queryXiaLa();

        return ResultBuildVO.success(user);
    }


    @Override
    public ResultVO queryPage(PlantSelectVo plantSelectVo) {
        List<PlantSelectVo> plantSelectVo1 = legalPersonMapper.queryPage(plantSelectVo);
        for (PlantSelectVo plant:plantSelectVo1) {
            Integer enabled = plant.getStatus() ? 1:0;
            plantSelectVo.setStateName(PersonEnum.getName(enabled));
        }
        int count = legalPersonMapper.count(plantSelectVo);
        AllRecordsVo allRecordsVo = new AllRecordsVo();
        allRecordsVo.setPageIndex(plantSelectVo.getPageIndex());
        allRecordsVo.setPageSize(plantSelectVo.getPageSize());
        allRecordsVo.setTotalSize(count);
        allRecordsVo.countPage(count);
        allRecordsVo.setDataList(plantSelectVo1);
        return ResultBuildVO.success(allRecordsVo);
    }
    //新增
    @Override
    public ResultVO add(LegalPersonAdd legalPersonAdd) {
        LegalPersonAdd legalPlantVo = legalPersonMapper.queryByCode(legalPersonAdd);
        if (legalPlantVo != null){
            return ResultBuildVO.error("编码已经存在");
        }
        Date now = new Date();
        LegalPerson legalPlantVo1 = new LegalPerson();
        BeanUtils.copyProperties(legalPersonAdd,legalPlantVo1);
        legalPlantVo1.setCreateName("从session中获取");
        legalPlantVo1.setCreateTime(now);
        legalPersonMapper.addPlant(legalPlantVo1);
        return ResultBuildVO.success();
    }

    @Override
    public ResultVO updatePlant(LegalPersonUpdate legalPersonUpdate) {
        if (legalPersonUpdate.getStatus()){
            return ResultBuildVO.error("只有在禁用状态才能修改");
        }
        Date now = new Date();
        LegalPerson legalPlant1 = new LegalPerson();
        BeanUtils.copyProperties(legalPersonUpdate,legalPlant1);
        legalPlant1.setUpdateName("从session获取");
        legalPlant1.setUpdateTime(now);
        int version = legalPersonMapper.updatePlant(legalPlant1);
        if (version==0){
            return ResultBuildVO.error("情刷新后修改");
        }
        return ResultBuildVO.success();
    }

    @Override
    public ResultVO updateStatus(LegalPersonUpdatestatus legalPersonUpdatestatus) {
        Date now = new Date();
        LegalPerson legalPlant1 = new LegalPerson();
        BeanUtils.copyProperties(legalPersonUpdatestatus,legalPlant1);
        legalPlant1.setUpdateName("从session获取");
        legalPlant1.setUpdateTime(now);
        legalPersonMapper.updateStatus(legalPlant1);
        return ResultBuildVO.success();

    }



}
